package com.yhp.vx.controller;

import com.yhp.bean.*;
import com.yhp.mvc.ResponseBody;
import com.yhp.service.impl.CourierService;
import com.yhp.service.impl.ExpressService;
import com.yhp.util.DateFormatUtil;
import com.yhp.util.GsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class ExpressController {
    @ResponseBody("/vx/express/list.do")
    public String vxExpressList(HttpServletRequest request,HttpServletResponse response){
        int info = (int) request.getSession().getAttribute("userInfo");
        System.out.println(info);
        Object user = request.getSession().getAttribute("user");
        System.out.println(user);
        String userPhone = null;
        if(info == 0){
            Courier c = (Courier) user;
            userPhone = c.getPhone();

        }else if(info == 1){
            User u = (User) user;
            userPhone = u.getPhone();
        }
        System.out.println(userPhone);

        List<Express> all = ExpressService.findByPhone(userPhone);
        Message msg = new Message();
        if(all.size() == 0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
            List<BootStrapExpress> list = new ArrayList<>();
            for (Express e : all) {
                String code = e.getCode() == null ? "已经取件" : e.getCode();
                String inTime = DateFormatUtil.format(e.getInTime());
                String outTime = e.getOutTime() == null ? "" : DateFormatUtil.format(e.getOutTime());
                String status = e.getStatus() == 0 ? "未出库" : "已出库";
                BootStrapExpress bootStrapExpress = new BootStrapExpress(e.getId(), e.getNumber(), e.getUserName(), e.getUserPhone(), e.getCompany(), code, inTime, outTime, status, e.getSysPhone());
                list.add(bootStrapExpress);
            }
            Stream<BootStrapExpress> data01 = list.stream().filter(express -> {
                if (express.getStatus().equals("未出库")) {
                    return true;
                } else {
                    return false;
                }
            }).sorted((o1, o2) -> {
                long l = DateFormatUtil.toTime(o1.getInTime());
                long l1 = DateFormatUtil.toTime(o2.getInTime());
                return (int) (l - l1);
            });
            Stream<BootStrapExpress> data02 = list.stream().filter(express -> {
                if (express.getStatus().equals("已出库")) {
                    return true;
                }else {
                    return false;
                }
            }).sorted((o1,o2) -> {
                long l = DateFormatUtil.toTime(o1.getInTime());
                long l1 = DateFormatUtil.toTime(o2.getInTime());
                return (int) (l-l1);
            });

            Object[] s0 = data01.toArray();
            Object[] s1 = data02.toArray();
            Map map = new HashMap();
            map.put("status0",s0);
            map.put("status1",s1);
            msg.setData(map);
        }

        String s = GsonUtil.toJSON(msg);


        return s;
    }


    @ResponseBody("/vx/userExpressList.do")
    public String expressList(HttpServletRequest request,HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        List<Express> all = ExpressService.findByPhoneAndStatus(userPhone, 0);
        List<BootStrapExpress> list = new ArrayList<>();
        for (Express e : all) {
            String code = e.getCode() == null ? "已经取件" : e.getCode();
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime() == null ? "" : DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus() == 0 ? "未出库" : "已出库";
            BootStrapExpress bootStrapExpress = new BootStrapExpress(e.getId(), e.getNumber(), e.getUserName(), e.getUserPhone(), e.getCompany(), code, inTime, outTime, status, e.getSysPhone());
            list.add(bootStrapExpress);
        }
        Message msg = new Message();
        if(list.size()==0){
            msg.setStatus(-1);
            msg.setResult("未查询到快递");
        }else {
            msg.setStatus(1);
            msg.setResult("查询成功");
            msg.setData(list);
        }
        return GsonUtil.toJSON(msg);
    }
}
